package org.lixf.controllers;

import org.lixf.beans.Student;
import org.lixf.services.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class LoginController {

    @Autowired
    private IStudentService studentService;

    public void doLogin(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String num = req.getParameter("num");
        String password = req.getParameter("password");
        HttpSession session = req.getSession();
        if (num == null || "".equals(num.trim())) {
            session.setAttribute("message", "学号不能为空");
            resp.sendRedirect(req.getContextPath() + "/login");
            return;
        }

        Student student = studentService.checkUser(num, password);
        if (student == null) {
            session.setAttribute("message", "学号或密码错误");
            resp.sendRedirect(req.getContextPath() + "/login");
            return;
        }
        resp.sendRedirect(req.getContextPath() + "/home.jsp");
    }
}
